package com.gp.controller.order;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gp.common.result.JsonResult;
import com.gp.common.utils.CookieUtils;
import com.gp.controller.BaseController;
import com.gp.model.dto.OrderDetailWithGoodsDto;
import com.gp.model.dto.OrderGoodsVO;
import com.gp.model.dto.PayInfoVo;
import com.gp.model.entity.BaseOrder;
import com.gp.model.entity.BaseOrdergoods;
import com.gp.model.entity.BaseUser;
import com.gp.model.entity.SubmitInfoVo;
import com.gp.service.IBaseCouponDistributionService;
import com.gp.service.IBaseOrderService;
import com.wordnik.swagger.annotations.Api;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 弓成龙
 * @since 2017-11-26
 */
@RestController
@Api(description = "订单", value = "订单")
@RequestMapping("/order")
public class OrderController extends BaseController {

  @Autowired
  private IBaseOrderService orderService;
  @Autowired
  IBaseCouponDistributionService couponService;
   
  /**
   * 获得用户所有订单
   * @param page  分页条件
   * @param isSerarch 是否搜索
   * @return
   */
  @RequestMapping(value = "/list", method = RequestMethod.GET) 
  public JsonResult list(HttpServletRequest request, Page<BaseOrder> page,@RequestParam(value="isSearch",required=false) Integer isSearch) {
    BaseUser user = (BaseUser)CookieUtils.obtainCurrentUser(request);
    Page<BaseOrder> list= null;
    if (user!=null && isSearch== null ) {
       list = orderService.getOrderList(page,user.getId());
    }else if(isSearch!=null && isSearch==1){
      Wrapper<BaseOrder> wrapper =new  EntityWrapper<BaseOrder>();
      Map<String, Object> map = page.getCondition();
      wrapper.like("orderCode", (String)map.get("orderCode"));
      page.setCondition(null);
      list = orderService.selectPage(page, wrapper);
      list.setCondition(map);
    }
     return new JsonResult("获取成功", list);
  }
  
  /**
   * 获得所有订单
   * @param userId
   * @return
   */
  @RequestMapping(value = "/listAll", method = RequestMethod.GET) 
  public JsonResult listAll(HttpServletRequest request, Page<BaseOrder> page) {
     page = orderService.selectPage(page);
     return new JsonResult("获取成功", page);
  }
  
  /**
   * 通过id获取订单
   * @param orderId
   * @return
   */
  @RequestMapping(value = "/detail/{orderId}", method = RequestMethod.GET)
  public JsonResult getById(@PathVariable String orderId) {
    BaseOrder order = orderService.selectById(Long.valueOf(orderId));
    return new JsonResult("获取成功", order);
  }
 
  /**
   * 通过未提交订单编码获取订单的详细信息
   * @param orderId 
   * @return
   */
  @RequestMapping(value = "/noSubmitOrder/{orderCode}", method = RequestMethod.GET)
  public JsonResult getByCode(@PathVariable String orderCode) {
    List<OrderGoodsVO> order = orderService.selectByCode(orderCode);
    return new JsonResult("获取成功", order);
  }
  /**
   * 通过订单编码获取订单的详细信息
   * @param orderId
   * @return
   */
  @RequestMapping(value = "/orderDetail/{orderCode}", method = RequestMethod.GET)
  public JsonResult getOrderByCodeSubmit(@PathVariable String orderCode) {
    List<OrderGoodsVO> order = orderService.getOrderByCodeSubmit(orderCode);
    return new JsonResult("获取成功", order);
  }
  
  
  /**
   * 通过订单编码获取订单的详细信息( 包括订单表本身的信息和订单商品表的信息)
   * @param orderId
   * @return
   */
  @RequestMapping(value = "/orderWithGoods/{orderCode}", method = RequestMethod.GET)
  public JsonResult getDetailByCode(@PathVariable String orderCode) {
    OrderDetailWithGoodsDto order = orderService.orderWithGoods(orderCode);
    return new JsonResult("获取成功", order);
  }
  
  /**
   * 创建订单
   * @param order
   * @return
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST) 
  public JsonResult add(BaseOrder order) {
    boolean result = orderService.addOrder(order);
    return result ? new JsonResult("创建成功", result) : new JsonResult("添加失败", result);
  }
  
  /**
   * 取消订单
   * @param orderId
   * @return
   */
  @RequestMapping(value = "/cancel/{orderId}", method = RequestMethod.POST) 
  public JsonResult cancel(@PathVariable String orderId) {
    boolean result = orderService.cancelOrder(Long.valueOf(orderId));
    return result ? new JsonResult("取消成功", result) : new JsonResult("取消失败", result);
  }

  /**
   * 删除订单
   * @param orderId
   * @return
   */
  @RequestMapping(value = "/del/{orderId}", method = RequestMethod.POST) 
  public JsonResult del(@PathVariable String orderId) {
    boolean result = orderService.deleteById(Long.valueOf(orderId));
    return result ? new JsonResult("删除成功", result) : new JsonResult("删除失败", result);
  }
  
  /**
   * 支付订单 这里使用平台币 进行支付
   * @param payInfo
   * @return
   */
  @RequestMapping(value = "/pay", method = RequestMethod.POST) 
  public JsonResult pay(@RequestBody PayInfoVo  payInfo) {
    // 设置订单中的支付相关的信息
    boolean result = orderService.pay(payInfo);
    return result ? new JsonResult("支付成功", result) : new JsonResult("支付失败", result);
  }
  
  /**
   * 密码确认
   * @param payInfo
   * @return
   */
  @RequestMapping(value = "/passwordConfirm", method = RequestMethod.POST) 
  public JsonResult passwordConfirm(HttpServletRequest request,String password) {
    BaseUser user = (BaseUser)CookieUtils.obtainCurrentUser(request);
    if(null == password || !StringUtils.hasText(password.trim()) || user== null) {
        return renderError("密码不正确");
     }
    // 设置订单中的支付相关的信息
    boolean result = orderService.passwordConfirm(user.getId(),password);
    return result ?renderSuccess("支付成功", result) :renderError("支付失败");
  }
  
  
  /**
   * 提交订单
   * @param submitInfo
   * @return
   */
  @RequestMapping(value = "/submit", method = RequestMethod.POST) 
  public JsonResult pay(@RequestBody SubmitInfoVo  submitInfo) {
    // 设置订单中的物流相关的信息
    boolean result = orderService.submit(submitInfo);
    return result ? renderSuccess("提交成功", result) : renderError("提交失败");
  }
  
  /**
   * 立即购买 ： 一条商品信息 + 一条订单信息
   * @param submitInfo 商品信息-订单信息封装
   * @return
   */
  @RequestMapping(value = "/imBuy", method = RequestMethod.POST) 
  public JsonResult imBuy(BaseOrdergoods dto) {
    BaseOrdergoods flag = orderService.imBuy(dto);
    return renderSuccess("操作成功",flag);
  }
    
}
